<?php

class Default_ViewController extends ExtLb_Controller_Action
{
	private $postTbl;
	private $commentTbl;
	private $menu2Tbl;

    public function init(){
        $this->loadCss('default', 'view', FOLDER_ROOT_NAME);

        $this->postTbl		= new Admin_Model_Posts();
        $this->commentTbl 	= new Admin_Model_Comments();
        $this->menu2Tbl		= new Admin_Model_Menu2();
        
        //RANDOM POST (BAI VIET NOI BAT)
    	$randPost = $this->postTbl->selectPost('bv.trangThai =1','baiVietId DESC',10)->query()->fetchAll();
    	$this->view->randPosts = $randPost;
    	
    	$this->view->comments = $this->commentTbl->selectComment(NULL,'yKienId DESC',5)->query()->fetchAll();
    }

    public function indexAction()
    {
    	if (isset($_GET['bv']) && !empty($_GET['bv'])){
    		$post = $this->postTbl->selectPost('baiVietId ='.$_GET['bv'])->query()->fetchAll();
    		
    		$this->view->post = $post;
    		if ($post[0]['menu2Id']==0){
    			$this->view->linkDirectory = 'Giới thiệu';
    		}else{
    			$linkDirectory = $this->menu2Tbl->selectMenu2('mn2.menu2Id ='.$post[0]['menu2Id'])->query()->fetchAll();
    			$this->view->linkDirectory = $linkDirectory;
    		}
    		
    		//BAI VIET LIEN QUAN
    		$like = explode(" ", $post[0]['tieuDe']);
    		$where = null;
    		
    		foreach ($like as $value) {
    			$where .= " tieuDe like '%".$value."%' or ";
    		}
    		$where  = substr($where,0,strlen($where)-3);
//    		 echo $where;
    		$this->view->postRelated = $this->postTbl->selectPost($where,'baiVietId DESC',8)
    												->query()->fetchAll();
    	}
    }


}

